package com.desmond.parallaxheaderviewpager;


import java.util.ArrayList;

import haitao.puzzle.R;
import hiatao.puzzle.adapter.PuzzleListAdapter;
import hiatao.puzzle.config.URLS;
import hiatao.puzzle.entity.PuzzleInfo;
import hiatao.puzzle.entity.User;
import hiatao.puzzle.utils.Tools;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ListView;


/**
 * A simple {@link Fragment} subclass.
 */
@SuppressLint("ValidFragment")
public class ListViewFragment extends ScrollTabHolderFragment {

    public static final String TAG = ListViewFragment.class.getSimpleName();
    private static final String ARG_POSITION = "position";
    private PuzzleListAdapter mAdapter;
    private ListView mListView;
    private int mPosition;
    private User user;
    private int index;
    public ListViewFragment() {
    }
    
  
    public ListViewFragment(int position,User user) {
   	 this.user=user;
   	 Bundle args = new Bundle();
   	index =position;
     args.putInt(ARG_POSITION, position);
     this.setArguments(args);
   }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_list_view, container, false);
        mListView = (ListView) view.findViewById(R.id.listview);
        View placeHolderView = inflater.inflate(R.layout.header_placeholder, mListView, false);
        mListView.addHeaderView(placeHolderView);

        mPosition = getArguments().getInt(ARG_POSITION);

        setAdapter();

        mListView.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {}

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

                if (mScrollTabHolder != null) {
                    mScrollTabHolder.onListViewScroll(
                            view, firstVisibleItem, visibleItemCount, totalItemCount, mPosition);
                }
            }
        });

        return view;
    }

    private void setAdapter() {
        if (getActivity() == null) return;
        mAdapter = new PuzzleListAdapter(user,getActivity());
        mListView.setAdapter(mAdapter);
        changeData();
    }
	private void changeData() {

		ArrayList<PuzzleInfo> list = new ArrayList<PuzzleInfo>();
		for (int i = 1; i < 5; i++) {
			PuzzleInfo info = new PuzzleInfo();
			info.setPicture(URLS.BASE_PICTURE + ((index) * 10 + 1 + i) + ".jpg");
			info.setPicture2(URLS.BASE_PICTURE + ((index) * 10 + 6 + i) + ".jpg");
			info.setJoinNum(Tools.getRandom(0, 100));
			info.setLikeNum(Tools.getRandom(0, 100));
			list.add(info);
		}
		mAdapter.clear();
		mAdapter.addItemLast(list);
	}
    @Override
    public void adjustScroll(int scrollHeight, int headerTranslationY) {
        if (mListView == null) return;

        if (scrollHeight == 0 && mListView.getFirstVisiblePosition() >= 1) {
            return;
        }

        mListView.setSelectionFromTop(1, scrollHeight);
    }
}
